package Fisica.Administracion;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Fisica.SessionFactoryUtil;
import Modelo.Iva;

public class ABMIvaFisica {

	//Busca un Cliente a partir de un identificador OK
    public static List<Iva> list() {
    	try {
    		SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory();
  			Session session = sessionFactory.getCurrentSession();
	    	Transaction transaction = session.beginTransaction();
		                
		    Query query = session
		     .createQuery("from Iva as i order by i.id asc");
		    List<Iva> list = query.list();
		        
		    transaction.commit(); //Como ya se ha hecho la consulta, hago commit. 
		        								   //El commit cierra automnticamente la session.
		    sessionFactory.close();
		
		    return list;	 
    	}catch (RuntimeException re) {
			System.out.println("get failed: "+re);
			throw re;
		}
   }

	public static Iva getById(Integer id) {
		//Busca un Cliente a partir de un identificador OK
	    	try {
	    		SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory();
	  			Session session = sessionFactory.getCurrentSession();
		    	Transaction transaction = session.beginTransaction();
			                
			    Query query = session
			     .createQuery("from Iva as i where i.id = "+id);
			    List<Iva> list = query.list();
			        
			    transaction.commit(); //Como ya se ha hecho la consulta, hago commit. 
			        								   //El commit cierra automnticamente la session.
			    sessionFactory.close();
			
			    if (!list.isEmpty() && list.get(0)!=null)
		        {
		        	return list.get(0);		        	
		        }	 
	    	}catch (RuntimeException re) {
				System.out.println("get failed: "+re);
				throw re;
			}
	    	return new Iva();
	   }
	}
    

